﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Models;

namespace Data_Object
{
    public class TaiKhoanDao:ITaiKhoanDao
    {
        public List<TaiKhoan> All()
        {
            string sql = "SELECT * FROM TaiKhoan";
            return Db.ReadList(sql, Make,null);
        }

        /// <summary>
        /// Creates a Category object based on DataReader.
        /// </summary>
        private static Func<IDataReader, TaiKhoan> Make = reader =>
           new TaiKhoan
           {
               MaTaiKhoan = int.Parse(reader["MaTaiKhoan"].ToString()),
               TenDangNhap = reader["TenDangNhap"].ToString(),
               MatKhau = reader["MatKhau"].ToString(),
               Email =reader["Email"].ToString(),
               DiaChi = reader["DiaChi"].ToString(),
               QuocGia = reader["QuocGia"].ToString(),
               DienThoai = reader["DienThoai"].ToString()
              
           };
       

        public TaiKhoan Find(int id)
        {
            string sql = "SELECT * FROM TaiKhoan WHERE MaTaiKhoan = @MaTaiKhoan";
            object[] parms = { "@MaTaiKhoan", id };
            return Db.Read(sql, Make, parms);

        }

        public void Insert(TaiKhoan entity)
        {
            string sql =
           @"INSERT INTO TaiKhoan (TenDangNhap,MatKhau,Email,DiaChi,QuocGia,DienThoai) 			
              VALUES (@TenDangNhap,@MatKhau,Email,@DiaChi,@QuocGia,@DienThoai)";
            entity.MaTaiKhoan = Db.Insert(sql, Take(entity));
        }

        private object[] Take(TaiKhoan entity)
        {
            // Note: Parameters in MS Access are positional. This means that the order of parameters 
            // should match the order in which parameters appear in the sql.
            return new object[]  
            {
                "@TenDangNhap", entity.TenDangNhap,
                "@MatKhau", entity.MatKhau,
                "@Email", entity.Email,
                "@DiaChi", entity.DiaChi,
                "@QuocGia", entity.MatKhau,
                "@DienThoai", entity.Email,
                "@MaTaiKhoan", entity.MaTaiKhoan
            };
        }

        public void Delete(int id)
        {
            string sql =
            @"DELETE FROM TaiKhoan
               WHERE MaTaiKhoan = @MaTaiKhoan";
            Db.Update(sql, new object[] { "@MaTaiKhoan", id });
        }

        public TaiKhoan ValidateUser(string username, string password)
        {
            string sql = "SELECT * FROM TaiKhoan WHERE TenDangNhap = @TenDangNhap AND MatKhau = @MatKhau";
            object[] parms = {
                                  "@TenDangNhap", username,
                                  "@MatKhau", password
                             };
            return Db.Read(sql, Make, parms);
        }

        public TaiKhoan FindByUserName(string username)
        {
            string sql = "SELECT * FROM TaiKhoan WHERE TenDangNhap = @TenDangNhap";
            object[] parms = {
                                  "@TenDangNhap", username
                             };
            return Db.Read(sql, Make, parms);
        }

        public void Update(TaiKhoan entity)
        {
            string sql =
            @"UPDATE TaiKhoan
                 SET TenDangNhap = @TenDangNhap,
                     MatKhau = @MatKhau,
                     Email = @Email,
                     DiaChi = @DiaChi,
                     QuocGia = @QuocGia                   
               WHERE MaTaiKhoan = @MaTaiKhoan";

            Db.Update(sql, Take(entity));
        }
    }
}
